package com.wf.smartcomm.server.weixin.action;

import java.net.URLDecoder;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.google.gson.Gson;
import com.wf.smartcomm.server.bean.Announcement;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.client.action.BaseAction;
import com.wf.smartcomm.server.service.AnnounceService;
import com.wf.smartcomm.server.service.UserService;
import com.wf.smartcomm.server.util.PushUtils;
import com.wf.smartcomm.server.weixin.util.WeixinUtil;

import net.sf.json.JSONObject;

@Controller("weixin_announceAction")
@Scope("prototype")
public class AnnouceAction extends BaseAction {
	
	@Resource
	private AnnounceService announceService;
	
	@Resource
	private UserService userService;
	
	public String list() throws Exception {
		String returnType="annouce";
		String openId="";
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user != null) {
//			page = announceService.list(user, page);
//			request.setAttribute("page", page);
//			request.setAttribute("announces", page.getDataList());
			return "success";
		}else {
//			Cookie[] cookies = request.getCookies();
//			if (cookies != null && cookies.length > 0) {
//				for(Cookie cookie : cookies){
//		            if(cookie.getName().equals("userId")){
//		            	String userId = cookie.getValue();
//		            	user = userService.getUserById(userId);
//		    			session.setAttribute("user",user);
////		    			page = announceService.list(user, page);
////		    			request.setAttribute("page", page);
////		    			request.setAttribute("announces", page.getDataList());
//		    			return "success";
//		            }
//		        }
//			}
			String code = request.getParameter("code");
			logger.info("code--------->"+code);
			String oauth_full_url ="";
			if (PushUtils.isDevEnv) {
				oauth_full_url=oauth_base_url_dev+"&code="+code+"&grant_type=authorization_code";
			}else {
				oauth_full_url=oauth_base_url_gen+"&code="+code+"&grant_type=authorization_code";
			}
			JSONObject jsonObject = WeixinUtil.httpRequest(oauth_full_url, "GET", null);
			if (jsonObject != null) {
				logger.info(jsonObject);
				openId = jsonObject.getString("openid");
				logger.info("openId-------->:"+openId);
				user = userService.getUserByOpenId(openId);
				if (user == null) {
//					Cookie cookie = new Cookie("openId", openId);
//					cookie.setMaxAge(3600*24*30*12*2);
//					response.addCookie(cookie);
					request.setAttribute("returnType", returnType);
					request.setAttribute("openId", openId);
					return "login";
				}
//				Cookie cookie = new Cookie("userId", user.getObjectId());
//				cookie.setMaxAge(3600*24*30*12*2);
//				response.addCookie(cookie);
				session.setAttribute("user", user);
//				page = announceService.list(user, page);
//				request.setAttribute("page", page);
//				request.setAttribute("announces", page.getDataList());
				return "success";
				
			}else {
				logger.info("jsonObject is null");
				request.setAttribute("returnType", returnType);
				request.setAttribute("openId", openId);
				return "login";
			}
		}
	}
	
	public void more()throws Exception{
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user == null) {
			String userId = request.getParameter("userId");
			if (StringUtils.isEmpty(userId)) {
				printJsonData(205);
				return;
			}else{
				user = userService.getUserById(userId);
			}
		}
		page = announceService.list(user, page);
		printJsonData(gson.toJson(page));
	}
	
	public String detail()throws Exception{
		Announcement announcement = announceService.get(request.getParameter("announceId"));
		announcement.setContent(URLDecoder.decode(announcement.getContent(), "UTF-8"));
		request.setAttribute("announce", announcement);
		return "detail";
	}

}
